package com.prs.crm.action.oa;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Synod;
import com.prs.crm.domain.oa.SynodRecord;
import com.prs.crm.domain.oa.SynodRoom;
import com.prs.crm.domain.oa.SynodType;
import com.prs.crm.service.oa.SynodRecordService;
import com.prs.crm.service.oa.SynodRoomService;
import com.prs.crm.service.oa.SynodService;
import com.prs.crm.service.oa.SynodTypeService;

public class SynodAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Synod synod;
	private SynodService synodService;
	private PaginationSupport pagination;
	private Integer[] synodIds;

	private SynodRecord synodRecord;
	private SynodRecordService synodRecordService;

	private File upload;
	private String uploadFileName;

	private String joinPersons;
	private String joinPersonNames;

	private List<SynodType> synodTypes;
	private List<SynodRoom> synodRooms;
	private SynodRoomService synodRoomService;
	private SynodTypeService synodTypeService;
	private String reg;

	@Action("listSynod")
	public String execute() {
		return SUCCESS;
	}

	@Action("listSynodByReg")
	public String listSynodByReg() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Synod.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (reg != null) {
			if (!"all".equals(reg)) {
				reg = reg.split(",")[0];
				if (ToolKits.isInteger(reg)) {
					criteria.createAlias("type", "ty");
					criteria.add(Restrictions
							.eq("ty.id", Integer.parseInt(reg)));
				}
			}
		}
		this.setPagination(this.getSynodService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listRecordSynod")
	public String listRecordSynod() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Synod.class);
		criteria.add(Property.forName("state").eq(Constrains.SYNOD_FITOUT));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSynodService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("alterSynod")
	public String alterSynod() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Synod.class);
		criteria.add(Property.forName("state").eq(Constrains.SYNOD_FITOUT));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSynodService().listByPage(criteria,
				pagination));

		return SUCCESS;
	}

	@Action("viewSynod")
	public String viewSynod() {
		Iterator<Person> iter = synod.getJoinPersons().iterator();
		while (iter.hasNext()) {
			Person person = iter.next();
			joinPersonNames = (joinPersonNames == null) ? person.getResume()
					.getName() : joinPersonNames + " , "
					+ person.getResume().getName();
			joinPersons = (joinPersons == null) ? person.getId().toString()
					: joinPersons + "," + person.getId().toString();
		}
		return SUCCESS;
	}

	@Action("editRecordSynod")
	public String editRecordSynod() {
		Iterator<Person> iter = synod.getJoinPersons().iterator();
		while (iter.hasNext()) {
			joinPersons = (joinPersons == null) ? iter.next().getResume()
					.getName() : joinPersons + " , "
					+ iter.next().getResume().getName();
		}
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitSynodRecord", results = { @Result(name = "success", type = "redirect", location = "listRecordSynod") })
	public String submitSynodRecord() {
		this.setPrompt("提交会议记录成功");
		synod.setState(Constrains.SYNOD_RECORD);
		this.getSynodService().save(synod);

		synodRecord.setSynod(synod);
		this.getSynodRecordService().saveSynodRecord(synodRecord, upload,
				uploadFileName);
		return SUCCESS;
	}

	@Action("editFitoutSynod")
	public String edit() {
		if (synod != null) {
			Iterator<Person> iter = synod.getJoinPersons().iterator();
			while (iter.hasNext()) {
				Person person = iter.next();
				joinPersonNames = (joinPersonNames == null) ? person
						.getResume().getName() : joinPersonNames + " , "
						+ person.getResume().getName();
				joinPersons = (joinPersons == null) ? person.getId().toString()
						: joinPersons + "," + person.getId().toString();
			}
		}
		synodTypes = this.getSynodTypeService().getAllSynodType();
		synodRooms = this.getSynodRoomService().getAllSynodRoom();
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "saveSynod", results = {
			@Result(name = "success", type = "redirect", location = "editFitoutSynod"),
			@Result(name = "error", type = "chain", location = "editFitoutSynod"),
			@Result(name = "none", type = "redirect", location = "alterSynod") })
	public String save() {
		if (synod.getTitle() == null || "".equals(synod.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (synod.getBeginTime() == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (synod.getEndTime() == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (synod.getSynodRoom() == null) {
			addActionError("地点不能为空");
			return ERROR;
		}
		if (synod.getType() == null) {
			addActionError("会议类型不能为空");
			return ERROR;
		}
		if (joinPersons == null || "".equals(joinPersons.trim())) {
			addActionError("参会人员不能为空");
			return ERROR;
		}
		if (synod.getId() == null) {
			this.setPrompt("提交会议准备成功");
			synod.setState(Constrains.SYNOD_FITOUT);
			this.getSynodService().saveSynod(synod, upload, uploadFileName,
					joinPersons);
			return SUCCESS;
		} else {
			this.setPrompt("变更会议准备成功");
			this.getSynodService().saveSynod(synod, upload, uploadFileName,
					joinPersons);
			return NONE;
		}

	}

	@Action(value = "removeSynod", results = {
			@Result(name = "success", type = "redirect", location = "listSynod"),
			@Result(name = "error", type = "chain", location = "listSynod") })
	public String remove() {
		if (synodIds == null) {
			this.addActionError("请选择要删除的议题");
			return ERROR;
		}
		this.getSynodService().batchRemove(synodIds);
		return SUCCESS;
	}

	public Synod getSynod() {
		return synod;
	}

	public void setSynod(Synod synod) {
		this.synod = synod;
	}

	public SynodService getSynodService() {
		return synodService;
	}

	public void setSynodService(SynodService synodService) {
		this.synodService = synodService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getSynodIds() {
		return synodIds;
	}

	public void setSynodIds(Integer[] synodIds) {
		this.synodIds = synodIds;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setJoinPersons(String joinPersons) {
		this.joinPersons = joinPersons;
	}

	public String getJoinPersons() {
		return joinPersons;
	}

	public SynodRecord getSynodRecord() {
		return synodRecord;
	}

	public void setSynodRecord(SynodRecord synodRecord) {
		this.synodRecord = synodRecord;
	}

	public List<SynodRoom> getSynodRooms() {
		return synodRooms;
	}

	public void setSynodRooms(List<SynodRoom> synodRooms) {
		this.synodRooms = synodRooms;
	}

	public SynodRoomService getSynodRoomService() {
		return synodRoomService;
	}

	public void setSynodRoomService(SynodRoomService synodRoomService) {
		this.synodRoomService = synodRoomService;
	}

	public SynodRecordService getSynodRecordService() {
		return synodRecordService;
	}

	public void setSynodRecordService(SynodRecordService synodRecordService) {
		this.synodRecordService = synodRecordService;
	}

	public void setJoinPersonNames(String joinPersonNames) {
		this.joinPersonNames = joinPersonNames;
	}

	public String getJoinPersonNames() {
		return joinPersonNames;
	}

	public List<SynodType> getSynodTypes() {
		return synodTypes;
	}

	public void setSynodTypes(List<SynodType> synodTypes) {
		this.synodTypes = synodTypes;
	}

	public SynodTypeService getSynodTypeService() {
		return synodTypeService;
	}

	public void setSynodTypeService(SynodTypeService synodTypeService) {
		this.synodTypeService = synodTypeService;
	}

	public String getReg() {
		return reg;
	}

	public void setReg(String reg) {
		this.reg = reg;
	}

}
